<template>
  <div class="event-apply" style="border: 2px;">
    <h2 style=" text-align: center;">事情申请流程</h2>
    <el-steps :active="1" align-center finish-status="success">
      <el-step title="成功提交申请"></el-step>
      <el-step title="待审核"></el-step>
      <el-step title="审核通过（不通过并附加理由说明）"></el-step>
    </el-steps>
    
    <el-form :model="form" ref="form" label-width="120px" class="form">
      <h2 style="text-align: center;">事件申请</h2>
      <el-form-item label="事件名称" required>
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="事件描述" required>
        <el-input v-model="form.content" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="附件上传">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
           multiple
           accept=".jpg,.png,.doc,.docx,.xlsx,.xls,.pptx,.txt"
          :data="{user_id: this.$store.getters.getUser.id,}"
          :file-list="fileList"
          :before-upload="beforeAvatarUpload"
          :on-success="handleSuccess"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :show-file-list="true"
         
        >
          <el-button type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png/txt/doc/pdf/excel文件，且不超过2MB
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button type="primary" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: '',
        content: '',
        status: '待审核'
      },
      fileName: '',
      attachmentUrl: [],
      fileList: [],
      uploadUrl: 'http://localhost:8086/upload' // 上传文件的接口地址
    };
  },
  methods: {
    //转换时间方法
    getDate() {
      //date是传过来的时间戳，注意需为13位，10位需*1000
      //也可以不传,获取的就是当前时间
      var time = new Date();
      var year = time.getFullYear()  //年
      var month = ("0" + (time.getMonth() + 1)).slice(-2); //月
      var day = ("0" + (time.getDate())).slice(-2); //日
      var mydate = year + "-" + month + "-" + day;
      return mydate
    },
    submitForm() {
      if(!this.$store.state.userInfo){
        this.$message.error("请登录后再进行申请！");
      }
      else{
      this.$refs.form.validate(valid => {
        if (valid) {
          // 提交表单数据到后端
          this.request.post('/event/apply', {
            title: this.form.title,
            content: this.form.content,
            attachment: this.attachmentUrl,
            user_id: this.$store.getters.getUser.id,
            time: this.getDate(),
            status: this.form.status
          })
            .then(response => {
              this.$message.success("提交成功！");
              // 重置表单
              this.form = {};
              this.fileName = '';
            })
            .catch(error => {
              this.$message.error(error.response.data.message);
            });
        } else {
          return false;
        }
      });
      }
    },
    resetForm() {
      // this.$refs.form.resetFields();
      this.form={};
    },
    beforeAvatarUpload(file) {
      //alert(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2MB!');
      }
      return  isLt2M;
    },
    handleSuccess(response) {
      this.$message.success('上传成功');
      console.log("存储路径："+response.virtualPath);
      console.log("文件名："+response.fileName)
      //this.fileName = response.fileName;
      this.attachmentUrl += response.fileName;
    },
    handleRemove(file, fileList) {
      if (file && file.status==="success") {
      this.request.post('remove?filename=' + file.name)
        .then(response => {
          console.log(file.name);
          this.$message.success("删除成功！");
          // 重置表单
          this.fileName = '';
          this.attachmentUrl = '';
        })
        .catch(error => {
          this.$message.error(error.response.data.message);
        });
      }
    },
    beforeRemove(file,fileList){
      if (file && file.status === "success") {
        return this.$confirm('确认移除文件--'+file.name)
      }
    }
  }
};
</script>

<style scoped>
.event-apply {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background:antiquewhite;
  padding: 1%;
}
.form{
            border-radius: 10px;
            margin: 0px auto;
            width: 450px;
            padding: 10px 35px 15px 35px;
            background: #fff;
            border: 1px solid #eaeaea;
            text-align: left;
            box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}
</style>
